/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package eu.livotov.tdv.core;

import com.sun.org.apache.xerces.internal.xs.PSVIProvider;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.event.ShortcutAction;
import com.vaadin.event.ShortcutListener;
import com.vaadin.ui.*;
import com.vaadin.ui.Button.ClickEvent;
import eu.livotov.tdv.DocumentViewer;
import eu.livotov.tdv.api.PageNumber;

import javax.swing.text.Keymap;

/**
 * @author dlivotov
 *
 *
 */
public abstract class DocumentViewerToolbar extends HorizontalLayout {

    public static float MAX_ZOOM = 3.0f;
    public static float MIN_ZOOM = 0.25f;
    public static float ZOOM_STEP = 0.25f;

    protected DocumentViewer viewer = null;
    protected Button zoomUp = new Button("+");
    protected Button zoomDown = new Button("-");
    protected Button btnPrevPage = new Button("<");
    protected Button btnNextPage = new Button(">");
    protected TextField zoomField = new TextField();
    protected TextField pageField = new TextField();
    protected float currentZoom = 1.0f;
    protected int currentPage = 1;
    protected int pageCount = 1;

    public DocumentViewerToolbar(DocumentViewer viewer) {
        super();
        this.viewer = viewer;
        setWidth("100%");
        setHeight(null);
        initUI();
        initActions();
    }

    protected abstract void initUI();

    protected void initStandartUI() {
        setSpacing(true);

        currentZoom = viewer.getZoom();
        currentPage = viewer.getCurrentPage();
        pageCount = viewer.getPagesCount();
        zoomField.setValue(currentZoom);
        zoomField.setEnabled(false);
        pageField.setEnabled(false);
        pageField.setValue(currentPage + "/" + pageCount);
        zoomDown.setWidth(null);
        zoomField.setWidth(null);
        zoomUp.setWidth(null);
        btnPrevPage.setWidth(null);
        btnPrevPage.setEnabled(false);
        pageField.setWidth(null);
        btnNextPage.setWidth(null);

        Label label = new Label();
        label.setWidth("100%");
        addComponent(zoomDown);
        addComponent(zoomField);
        addComponent(zoomUp);
        addComponent(btnPrevPage);
        addComponent(pageField);
        addComponent(btnNextPage);
        addComponent(label);
        setExpandRatio(label, 1.0f);
    }

    private void initActions() {
//        zoomUp.setClickShortcut(ShortcutAction.);
        zoomUp.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                currentZoom = viewer.getZoom();
                if (currentZoom + ZOOM_STEP <= MAX_ZOOM) {
                    viewer.setZoom(currentZoom + ZOOM_STEP);
                    zoomField.setValue(currentZoom + ZOOM_STEP);
                    zoomDown.setEnabled(true);
                } else {
                    zoomUp.setEnabled(false);
                }
            }
        });

        zoomDown.addListener(new Button.ClickListener() {

            public void buttonClick(ClickEvent event) {
                currentZoom = viewer.getZoom();
                currentZoom = viewer.getZoom();
                if (currentZoom - ZOOM_STEP >= MIN_ZOOM) {
                    viewer.setZoom(currentZoom - ZOOM_STEP);
                    zoomField.setValue(currentZoom - ZOOM_STEP);
                    zoomUp.setEnabled(true);
                } else {
                    zoomDown.setEnabled(false);
                }
            }
        });
        btnPrevPage.setClickShortcut(ShortcutAction.KeyCode.PAGE_UP);
        btnPrevPage.addListener(new Button.ClickListener() {

            public void buttonClick(Button.ClickEvent clickEvent) {
                currentPage = viewer.getCurrentPage();
                if (currentPage-- > 0) {
                    viewer.goPage(currentPage);
                    btnNextPage.setEnabled(true);
                } else {
                    viewer.goPage(0);
                    btnPrevPage.setEnabled(false);
                }
                pageField.setValue(currentPage + "/" + pageCount);
            }
        });


        btnNextPage.setClickShortcut(ShortcutAction.KeyCode.PAGE_DOWN);
        btnNextPage.addListener(new Button.ClickListener() {

            public void buttonClick(Button.ClickEvent clickEvent) {
                currentPage = viewer.getCurrentPage();
                if (currentPage++ < viewer.getPagesCount()-1) {
                    viewer.goPage(currentPage);
                    btnPrevPage.setEnabled(true);
                } else {
                    viewer.goPage(currentPage);
                    btnNextPage.setEnabled(false);
                }
                pageField.setValue(currentPage + "/" + pageCount);
            }
        });

    }

    protected void goPage(int pageNumber){
        if ( pageNumber>= PageNumber.FIRST_PAGE && pageNumber<=pageCount)
        {
            currentPage = (pageNumber == PageNumber.LAST_PAGE) ? pageCount : pageNumber;
            viewer.goPage(currentPage);
        }
    }

    public void setCurrentPageNumber(int pageNumber) {
        currentPage = pageNumber;
        if(currentPage > 1 )btnPrevPage.setEnabled(true);
        else btnPrevPage.setEnabled(false);
        if(currentPage < pageCount )btnNextPage.setEnabled(true);
        else btnNextPage.setEnabled(false);
        pageField.setValue(currentPage + "/" + pageCount);
    }
}
